function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function LoadBoutData(url) {
    $.ajax({
      url: url,
      success: function( data ) {
          loadDerbyXML(data);
      },
      dataType: "xml"
    });
    $(".content-holder").hide();
    $("#content-bout").show();
}
function ReloadBoutData(url) {
    $("#overviewPage").parent().html("");
    $("#jamsPage").parent().html("");
    $("#skatersPage").parent().html("");
    LoadBoutData(url);
    // force the reload (ugh!!)
    $("#tabs").tabs("option","active",1);
    $("#tabs").tabs("option","active",0);
}
function switchIndex(name, builder) {
    if (! $('#' + name + 'Index').length) {
        $.get(name + "Index.html", function (data) {
            $("#content-" + name).html(data);
            builder()
        });
    }
    $(".content-holder").hide();
    $("#content-" + name).show();
}
$(function() {
//$(".menuBarItem ul").css({width:"200px", 'padding-right':"20px"});
//$(".menuBarItem").accordion({ collapsible: true, active: false });
//$(".menuBarItem div").removeClass("ui-accordion-content ui-corner-all ui-corner-bottom ui-widget-content");
$( ".menuBarItem" ).button();
$( "#boutMenu").click(function () {
    switchIndex("bouts",createBoutsIndex);
})
$( "#teamsMenu").click(function () {
    switchIndex("teams",createBoutsIndex);
})
$( "#skatersMenu").click(function () {
    switchIndex("skaters",createSkatersIndex);
})
$( "#helpMenu").click(function () {
    switchIndex("help",function() {
         $( "#helpTabs" ).tabs();
     });
})
//  $( "#rootTabs" ).tabs();
  $( "#tabs" ).tabs({load: function( event, ui ) {
      if ($(ui.tab).text() == "Overview") {
          prepareOverviewPage();
      }
    if ($(ui.tab).text() == "Jams") {
        prepareJamsPage();
    }
    if ($(ui.tab).text() == "Skaters") {
        $("#skatersPageFilter").buttonset();
        $('#skatersPageFilter input[type=radio]').change(function() {
             filterSkatersPage(this.id);
        })        
        prepareSkatersPage();
    }
  }});
  
  $('#teamsMenu').hide();
  $('#skatersMenu').hide();
  
  var opts = getUrlVars();
  if (!opts.league) {
      opts.league = DXML.defaultLeague;
  }
    if (!opts.bout) {
        opts.bout = DXML.defaultBout;
    }
    if (opts.league) {
        $.ajax({
          url: "DerbyXMLFiles/"+opts.league+".derbyxml",
          success: function( data ) {
              loadLeagueXML(data);
              // load the bout after league info
                if (opts['bout']) {
                  LoadBoutData("DerbyXMLFiles/"+opts.bout+".derbyxml");
                } else {
                    switchIndex("bouts",createBoutsIndex);
                }
          },
          dataType: "xml"
        });
    } else {
        $('#boutMenu').hide();
        $('#teamsMenu').hide();
        $('#skatersMenu').hide();
        if (opts.bout) {
          LoadBoutData("DerbyXMLFiles/"+opts.bout+".derbyxml");
        }
    }
});
